package cn.zqh.store.service.Impl;

import cn.zqh.bean.api.CommonResult;
import cn.zqh.bean.api.ResultCode;
import cn.zqh.bean.bo.*;
import cn.zqh.store.config.BSException;
import cn.zqh.store.holder.LoginUserHolder;
import cn.zqh.uuid.utils.SnowflakeId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zqh.store.mapper.StoreMapper;
import cn.zqh.store.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zqh
 * @since 2023-01-06
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements StoreService {
    
    @Resource
    private StoreMapper storeMapper;

    @Autowired
    private LoginUserHolder loginUserHolder;
    
    
    /**
      * @Author: zqh
      * @Date: 
      * @Description: 通过用户id查询店铺
    */
    @Override
    public CommonResult QueryTheStoresUnderTheCompanyThroughTheUserPrimaryKey(String uid) {
        Store store = storeMapper.selectOne(new QueryWrapper<Store>().eq("User_id", uid));
        if (store == null) {
            return CommonResult.failed();
        }
        return CommonResult.success(store);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 添加一个新店铺
    */
    @Override
    public CommonResult addAAewStore(Pstore pstore) {
        UserDTO currentUser = loginUserHolder.getCurrentUser();
        Store store = new Store();
        //雪花算法生成主键
        long l = SnowflakeId.nextId();
        store.setStoreId("store:"+l)
                .setUserId((String) currentUser.getId())
                .setStoreName(pstore.getStoreName())
                .setIntro(pstore.getIntro())
                .setAvatar(pstore.getAvatar())
                .setStoreStyle(0)
                .setIsDelete(0);
        int insert = storeMapper.insert(store);
        if (insert != 1){
            return CommonResult.failed(ResultCode.FAILED);
        }else {
            return CommonResult.success(ResultCode.SUCCESS);
        }
    }
    /**
      * @Author: zqh
      * @Date: 
      * @Description: 查询所有店铺
    */
    @Override
    public CommonResult getAllStore() {
        List<Store> stores = storeMapper.selectList(null);
        return CommonResult.success(stores);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 通过店铺id获取店铺信息
    */
    @Override
    public CommonResult getStoreInformationByStoreId(String storeId) {
        Store store = storeMapper.selectOne(new QueryWrapper<Store>().eq("Store_id", storeId));
        if (store == null) {
            throw new BSException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMessage());
        }
        return CommonResult.success(store);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 店主修改店铺信息
    */
    @Override
    public CommonResult storeOwnerModifiesStoreInformation(UserChangeStore userChangeStore) {
        Store store = storeMapper.selectOne(new QueryWrapper<Store>().eq("Store_id", userChangeStore.getStoreId()));
        if (store == null){
            throw new BSException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMessage());
        }
        store.setStoreName(userChangeStore.getStoreName())
                .setIntro(userChangeStore.getIntro())
                .setAvatar(userChangeStore.getAvatar());
        int i = storeMapper.updateById(store);
        if (i != 1){
            throw new BSException(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
        }
        return CommonResult.success(ResultCode.SUCCESS);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 管理员修改店铺信息
    */
    @Override
    public CommonResult adminModifiesStoreInformation(AdminChangeStore adminChangeStore) {
        Store store = storeMapper.selectOne(new QueryWrapper<Store>().eq("Store_id", adminChangeStore.getStoreId()));
        if (store == null){
            throw new BSException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMessage());
        }
        store.setStoreName(adminChangeStore.getStoreName())
                .setIntro(adminChangeStore.getIntro())
                .setStoreStyle(adminChangeStore.getStoerStyle())
                .setAvatar(adminChangeStore.getAvatar());
        int i = storeMapper.updateById(store);
        if (i != 1){
            throw new BSException(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
        }
        return CommonResult.success(ResultCode.SUCCESS);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 删除店铺
    */
    @Override
    public CommonResult delStoreById(String storeId) {
        int i = storeMapper.deleteById(storeId);
        if (i != 1){
            throw new BSException(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
        }
        return CommonResult.success(ResultCode.SUCCESS);
    }
}
